﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Infragistics.Win.UltraWinDataSource;
using ATA.Data.MisaDAL;
using Infragistics.Win.UltraWinEditors;
using Infragistics.Win.UltraWinGrid;

using ATA.Entities;
using ATA.Business;
using ATA.Data;

namespace ATA.Winforms
{
    public partial class frmSearchDangkyLapMayPhaMau : Form
    {
        public frmSearchDangkyLapMayPhaMau()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void frmSearchDangkyLapMayPhaMau_Load(object sender, EventArgs e)
        {
            LoadCustomerList();
            LoadBrandList();
        }

        private void LoadBrandList()
        {
            //ThuongHieuService v_srvThuongHieu = new ThuongHieuService();
            //TList<ThuongHieu> v_lsThuongHieu = v_srvThuongHieu.Find("IsActive = TRUE");

            cboBrandNames.ValueMember = "BrandID";
            cboBrandNames.DisplayMember = "BrandName";
            cboBrandNames.DataSource = Globals.gv_lsThuongHieu;
            cboBrandNames.DataBind();
        }

        private void LoadCustomerList()
        {
            foreach (AccountingObject v_lsParentCusomers in Globals.gv_lsCustomers)
            {
                UltraDataRow row = dsCustomers.Rows.Add();
                row["AccountingObjectId"] = v_lsParentCusomers.AccountingObjectId.ToString();
                row["ParentAccountingObjectId"] = v_lsParentCusomers.ParentAccountingObjectId.ToString();
                row["AccountingObjectName"] = v_lsParentCusomers.AccountingObjectName;
                row["AccountingObjectCode"] = v_lsParentCusomers.AccountingObjectCode;
                foreach (AccountingObject v_lsChildCusomers in Globals.gv_lsCustomersNotInMisa)
                {
                    if (v_lsChildCusomers.ParentAccountingObjectId.Equals(v_lsParentCusomers.AccountingObjectId))
                    {
                        UltraDataRowsCollection childRows = row.GetChildRows("Band 1");
                        UltraDataRow childRow = childRows.Add();
                        childRow["ParentAccountingObjectId"] = v_lsChildCusomers.ParentAccountingObjectId.ToString();
                        childRow["AccountingObjectId"] = v_lsChildCusomers.AccountingObjectId.ToString();
                        childRow["AccountingObjectName"] = v_lsChildCusomers.AccountingObjectName;
                        childRow["AccountingObjectCode"] = v_lsChildCusomers.AccountingObjectCode;
                    }
                }

            }

            grdCustomers.DataSource = dsCustomers;
            grdCustomers.DisplayLayout.Bands[0].Columns["AccountingObjectID"].Hidden = true;
            grdCustomers.DisplayLayout.Bands[0].Columns["ParentAccountingObjectID"].Hidden = true;
            grdCustomers.DisplayLayout.Bands[0].Columns["AccountingObjectCode"].Header.Caption = "Mã KH";
            grdCustomers.DisplayLayout.Bands[0].Columns["AccountingObjectCode"].Width = 120;
            grdCustomers.DisplayLayout.Bands[0].Columns["AccountingObjectName"].Header.Caption = "Tên KH";
            grdCustomers.DisplayLayout.Bands[0].Columns["AccountingObjectName"].Width = 250;
            grdCustomers.DisplayLayout.Bands[1].Columns["AccountingObjectID"].Hidden = true;
            grdCustomers.DisplayLayout.Bands[1].Columns["ParentAccountingObjectID"].Hidden = true;
            grdCustomers.DisplayLayout.Bands[1].Columns["AccountingObjectCode"].Header.Caption = "Mã KH";
            grdCustomers.DisplayLayout.Bands[1].Columns["AccountingObjectCode"].Width = 120;
            grdCustomers.DisplayLayout.Bands[1].Columns["AccountingObjectName"].Header.Caption = "Tên KH";
            grdCustomers.DisplayLayout.Bands[1].Columns["AccountingObjectName"].Width = 250;
            grdCustomers.Width = 520;
            grdCustomers.DataBind();

            DropDownEditorButton objKhachHang = new DropDownEditorButton();
            objKhachHang.Control = this.grdCustomers;
            txtCustomers.ButtonsRight.Add(objKhachHang);
        }

        private void grdCustomers_DoubleClickRow(object sender, Infragistics.Win.UltraWinGrid.DoubleClickRowEventArgs e)
        {
            txtCustomers.Focus();
        }

        private void txtCustomers_AfterEditorButtonCloseUp(object sender, EditorButtonEventArgs e)
        {
            if (grdCustomers.ActiveRow.Index > -1)
            {
                if (grdCustomers.ActiveRow.Cells["AccountingObjectID"].Value != null)
                {
                    txtCustomers.Text = grdCustomers.ActiveRow.Cells["AccountingObjectCode"].Value.ToString();
                    txtCustomerName.Text = grdCustomers.ActiveRow.Cells["AccountingObjectName"].Value.ToString();
                    //LoadDataByCustomer(new Guid(grdCustomers.ActiveRow.Cells["AccountingObjectID"].Value.ToString()));
                }
            }
        }

        private void LoadDataByCustomer(Guid pv_guidCustomerID)
        {
            KhachHangDuthuongService v_srvKhachHangDuThuong = new KhachHangDuthuongService();
            KhachHangDuthuong v_entKhachHangDuThuong = v_srvKhachHangDuThuong.GetByCustomerID(pv_guidCustomerID);

            if (v_entKhachHangDuThuong != null)
            {
                rdoLoaiThoigian.Value = v_entKhachHangDuThuong.Duration;
                dtmStartDate.Value = v_entKhachHangDuThuong.StartDate;
                dtmEndDate.Value = v_entKhachHangDuThuong.EndDate;
                cboBrandNames.Value = v_entKhachHangDuThuong.BrandId;

                DoanhSoCamKetService v_srvDoanhSoCamKet = new DoanhSoCamKetService();
                TList<DoanhSoCamKet> v_lsDoanhSoCamKet = v_srvDoanhSoCamKet.GetByCustomerID(v_entKhachHangDuThuong.Id);

                grdResults.DataSource = v_lsDoanhSoCamKet;
                grdResults.DataBind();

                //grdDoanhsoCamket.DisplayLayout.Bands[0].Columns["Doanhso"].CellActivation = Activation.AllowEdit;
                //grdDoanhsoCamket.DisplayLayout.Bands[0].Columns["Doanhso"].Header.Caption = "Doanh số";
                //grdDoanhsoCamket.DisplayLayout.Bands[0].Columns["Year"].Header.Caption = "Năm";
                //grdDoanhsoCamket.DisplayLayout.Bands[0].Columns["Quarter"].Header.Caption = "Quý";
                //grdDoanhsoCamket.DisplayLayout.Bands[0].Columns["Month"].Header.Caption = "Tháng";
                //grdDoanhsoCamket.DisplayLayout.Bands[0].Columns["Month"].Header.Caption = "Tháng";
                //grdDoanhsoCamket.DisplayLayout.Bands[0].Columns["ID"].Hidden = true;
                //grdDoanhsoCamket.DisplayLayout.Bands[0].Columns["KhachHangDuThuongID"].Hidden = true;
                //grdDoanhsoCamket.DisplayLayout.Bands[0].Columns["IsActive"].Hidden = true;
            }
            else
            {
                rdoLoaiThoigian.Value = null;
                dtmStartDate.Value = null;
                dtmEndDate.Value = null;
                cboBrandNames.Value = null;

                grdResults.DataSource = null;
                grdResults.DataBind();
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                frmDangkyLapmayPhamau v_frmDangKyLapMay = new frmDangkyLapmayPhamau();
                v_frmDangKyLapMay.Show(this);
            }
            catch (Exception ex)
            {
                //MessageBox.Show(this, "Không cập nhật được thông tin!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            try
            {
                string v_strCondition = "1 = 1";
                int v_iTotalRows = 0;

                if (txtSobienban.Text.Trim() != string.Empty)
                {
                    v_strCondition += " AND SoBienBan LIKE '%" + txtSobienban.Text + "%' ";
                }

                if (Convert.ToInt16(rdoLoaiMay.Value) > 0)
                {
                    v_strCondition += " AND LoaiMay = " + rdoLoaiMay.Value;
                }

                //if (cboBrandNames.SelectedIndex != -1)
                //{
                //    v_strCondition += " AND BrandID = " + cboBrandNames.Value;
                //}

                //if (Convert.ToInt16(rdoLoaiThoigian.Value) > 0)
                //{
                //    v_strCondition += " AND Duration = " + rdoLoaiThoigian.Value;
                //}

                v_strCondition += " AND StartDate >= CONVERT(datetime, '" + dtmStartDate.DateTime.Month + "/01/" + dtmStartDate.DateTime.Year + "', 101)";   // mm/dd/yyyy

                //KhachHangDuthuongService v_srvKhachHangDuThuong = new KhachHangDuthuongService();

                DataSet v_dsKhachHangDuThuong = DataRepository.Provider.ExecuteDataSet("KhachHangLapMayPhaMau_GetListByCondition", v_strCondition);

                //Create the Dataset from the custom method
                //DataSet dsOrderList = DataRepository.KhachHangDuthuongProvider.GetListByCondition(();


                grdResults.DataSource = v_dsKhachHangDuThuong;
                grdResults.DataBind();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "Có lỗi xảy ra!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void grdResults_DoubleClickRow(object sender, DoubleClickRowEventArgs e)
        {
            string v_strKhachHangDuThuongID = e.Row.Cells["ID"].Value.ToString();
            frmDangkyLapmayPhamau v_frmDangKyLapMay = new frmDangkyLapmayPhamau(v_strKhachHangDuThuongID);
            v_frmDangKyLapMay.Show(this);
        }
    }
}
